<?php

namespace Admin\Controller;

class SliderController extends AdminController {

    public function index() {
        $total = M('Slider')->count();
        $Page = new \Think\Page($total, 20);
        $result = M('Slider')->page($Page->nowPage, $Page->listRows)->order('id DESC')->select();
        foreach ($result as $key => $val) {
            $result[$key]['total'] = M('SliderItem')->where(['slider_id' => $val['id']])->count();
        }
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $name = I('name');
            $code = I('code');
            if (!$name) {
                $this->error('请输入幻灯片名称');
            }
            if (!$code) {
                $this->error('请输入调用名称');
            }
            if (!preg_match('/^[a-zA-Z0-9\_]+$/', $code)) {
                $this->error('调用名称非法');
            }
            $total = M('Slider')->where(['code' => $code])->count();
            if ($total) {
                $this->error('调用名称已存在');
            }
            M('Slider')->data(['name' => $name, 'code' => $code])->add();
            $this->success('添加成功');
        } else {
            $this->display();
        }
    }

    public function edit() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $name = I('name');
            $code = I('code');
            if (!$name) {
                $this->error('请输入幻灯片名称');
            }
            if (!$code) {
                $this->error('请输入调用名称');
            }
            if (!preg_match('/^[a-zA-Z0-9\_]+$/', $code)) {
                $this->error('调用名称非法');
            }
            $total = M('Slider')->where(['code' => $code, 'id' => ['neq', $id]])->count();
            if ($total) {
                $this->error('调用名称已存在');
            }
            M('Slider')->data(['name' => $name, 'code' => $code, 'id' => $id])->save();
            $this->success('编辑成功');
        } else {
            $result = M('Slider')->find($id);
            $this->assign($result);
            $this->display();
        }
    }
    
    public function delete(){
        $id = I('id',0,'intval');
        M('Slider')->where(['id'=>$id])->delete();
        M('SliderItem')->where(['slider_id'=>$id])->delete();
        $this->success('删除成功');
    }

    public function item() {
        $slider_id = I('slider_id', 0, 'intval');
        $result = M('SliderItem')->where(['slider_id' => $slider_id])->order('sort ASC')->select();
        $slider_info = M('Slider')->find($slider_id);
        $this->assign('result', $result);
        $this->assign('slider_id', $slider_id);
        $this->assign('slider_info', $slider_info);
        $this->display();
    }

    public function item_add() {
        $slider_id = I('slider_id', 0, 'intval');
        if (IS_POST) {
            $data = I('post.');
            if (!$_FILES['pic']) {
                $this->error('请上传图片');
            }
            if ($data['start_time']) {
                $data['start_time'] = strtotime($data['start_time']);
            }
            if ($data['end_time']) {
                $data['end_time'] = strtotime($data['end_time']);
            }
            $data['status'] = 1;
            $data['sort'] = 0;
            $data['pic'] = $this->uploadImage($_FILES['pic']);
            if (M('SliderItem')->data($data)->add()) {
                $this->success('添加成功',U('training/index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $slider_info = M('Slider')->find($slider_id);
            $this->assign('slider_id', $slider_id);
            $this->assign('slider_info', $slider_info);
            $this->display();
        }
    }

    public function item_edit() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $data = I('post.');
            if ($data['start_time']) {
                $data['start_time'] = strtotime($data['start_time']);
            }
            if ($data['end_time']) {
                $data['end_time'] = strtotime($data['end_time']);
            }

            if ($_FILES['pic']) {
                $data['pic'] = $this->uploadImage($_FILES['pic']);
            } else {
                unset($data['pic']);
            }
            if (M('SliderItem')->data($data)->save() !== false) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $result = M('SliderItem')->find($id);
            $slider_info = M('Slider')->find($result['slider_id']);
            $this->assign($result);
            $this->assign('slider_info', $slider_info);
            $this->display();
        }
    }

    public function item_sort() {
        $sort = I('sort', 0, 'intval');
        if (!is_array($sort)) {
            $sort = [$sort];
        }
        foreach ($sort as $id => $val) {
            M('SliderItem')->where(['id' => $id])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    public function item_valid() {
        $id = I('id', 0, 'intval');
        M('SliderItem')->where(['id' => $id])->setField('status', 1);
        $this->success('操作成功');
    }

    public function item_invalid() {
        $id = I('id', 0, 'intval');
        M('SliderItem')->where(['id' => $id])->setField('status', 0);
        $this->success('操作成功');
    }
    
    public function item_delete(){ 
         $id = I('id', 0, 'intval');
        M('SliderItem')->where(['id' => $id])->delete();
        $this->success('删除成功');   
    }

}
